﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DataGridHelper.cs" company="SmartPlan">
//   SmartPlan 2011
// </copyright>
// <summary>
//   The data grid helper.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace DbDiff.WinClient.Forms.DbItems
{
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;
    using Common.DataAccess;
    using Controls;

    /// <summary>
    /// The data grid helper.
    /// </summary>
    public class DataGridHelper
    {
        #region FirstColum enum

        /// <summary>
        /// The first colum.
        /// </summary>
        public enum FirstColum
        {
            /// <summary>
            /// The all.
            /// </summary>
            All = -1, 

            /// <summary>
            /// The second.
            /// </summary>
            Second = 1
        }

        #endregion

        /// <summary>
        /// The table style.
        /// </summary>
        /// <param name="data">
        /// The data.
        /// </param>
        /// <param name="pcol">
        /// The pcol.
        /// </param>
        /// <param name="dgDbDiffList">
        /// The dg db diff list.
        /// </param>
        /// <param name="firstColumn">
        /// The first column.
        /// </param>
        /// <returns>
        /// </returns>
        public static DataGridTableStyle TableStyle(DataTable data, PropertyDescriptorCollection pcol, 
                                                    DataGrid dgDbDiffList, FirstColum firstColumn)
        {
            var dataMsg = new ErrorMessageTables();
            var tableStyle = new DataGridTableStyle();
            DataGridColumnStyle columnStyle;

            tableStyle.MappingName = data.TableName;
            tableStyle.RowHeadersVisible = false;
            tableStyle.AllowSorting = true;
            tableStyle.ReadOnly = false;
            string captionName;


            if (UcDbDiffList.isDiff(data))
            {
                columnStyle = new DataGridImageColumn(pcol["Msg"])
                                  {
                                      MappingName = "Msg",
                                      HeaderText = string.Empty,
                                      Width = 20
                                  };

                tableStyle.GridColumnStyles.Add(columnStyle);
            }

            int i = 0;

            foreach (DataColumn column in data.Columns)
            {
                // schema név és tábla név nem kell a részletezéshez
                if ((!UcDbDiffList.isDiff(data) & i > (int)firstColumn)
                    | UcDbDiffList.isDiff(data))
                {
                    captionName = GetCaptionName(dataMsg, data.TableName, column.ColumnName, column.Caption);
                    if (captionName.ToLower() != "false")
                    {
                        if (column.DataType == typeof(bool))
                        {
                            columnStyle = new DataGridBoolColumn(pcol[column.ColumnName]) { ReadOnly = true };
                        }
                        else
                        {
                            columnStyle = new DataGridROTextboxColumn(pcol[column.ColumnName]) { ReadOnly = true };
                        }

                        int longField1 = LongestField(dgDbDiffList, data.DefaultView, column.ColumnName);
                        int index = dataMsg.FindIndexByName(data.TableName);
                        int longField2 = LongestField(dgDbDiffList, dataMsg.Tables[index].DefaultView, column.ColumnName);
                        columnStyle.Width = longField1 > longField2 ? longField1 : longField2;

                        columnStyle.MappingName = column.ColumnName;
                        columnStyle.HeaderText = captionName;

                        // ha a selectfor compare, akkor azt lehet állítani
                        if (column.ColumnName.ToUpper() == "SELECTFORCOMPARE")
                        {
                            column.ReadOnly = false;
                        }


                        columnStyle.ReadOnly = false;
                        tableStyle.GridColumnStyles.Add(columnStyle);
                    }
                }

                i++;
            }

            return tableStyle;
        }

        /// <summary>
        /// The get caption name.
        /// </summary>
        /// <param name="dataMsg">
        /// The data msg.
        /// </param>
        /// <param name="tableName">
        /// The table name.
        /// </param>
        /// <param name="columnName">
        /// The column name.
        /// </param>
        /// <param name="defaultValue">
        /// The default value.
        /// </param>
        /// <returns>
        /// The get caption name.
        /// </returns>
        public static string GetCaptionName(ErrorMessageTables dataMsg, string tableName, string columnName, 
                                            string defaultValue)
        {
            string s = defaultValue;
            int index = dataMsg.FindIndexByName(tableName);
            if (index > -1)
            {
                if (dataMsg.Tables[index].Columns.Contains(columnName))
                {
                    s = dataMsg.Tables[index].Columns[columnName].Caption;
                }
            }

            return s;
        }

        /// <summary>
        /// The longest field.
        /// </summary>
        /// <param name="dgDbDiffList">
        /// The dg db diff list.
        /// </param>
        /// <param name="data">
        /// The data.
        /// </param>
        /// <param name="columnName">
        /// The column name.
        /// </param>
        /// <returns>
        /// The longest field.
        /// </returns>
        public static int LongestField(DataGrid dgDbDiffList, DataView data, string columnName)
        {
            int maxlength = 0;
            int tot = data.Count;
            string straux = string.Empty;
            int intaux = 0;

            Graphics g = dgDbDiffList.CreateGraphics();

            int offset = Convert.ToInt32(Math.Ceiling(g.MeasureString(" ", dgDbDiffList.Font).Width));

            maxlength = Convert.ToInt32(Math.Ceiling(g.MeasureString(columnName, dgDbDiffList.Font).Width));
            for (int i = 0; i < tot; ++i)
            {
                straux = data[i][columnName].ToString();

                // Get the width of Current Field String according to the Font
                intaux = Convert.ToInt32(Math.Ceiling(g.MeasureString(straux, dgDbDiffList.Font).Width));
                if (intaux > maxlength)
                {
                    maxlength = intaux;
                }
            }
            // End of For Loop

            return maxlength + offset;
        }
    }
}